package org.rcgwt.client.ui.validator.impl;

import org.rcgwt.client.ui.validator.IChecker;
import org.rcgwt.client.ui.validator.IClientValidatorContext;
import org.rcgwt.client.ui.validator.ISingleton;
import org.rcgwt.client.ui.validator.IValidatorElement;
import org.rcgwt.client.ui.validator.messages.Severity;
import org.rcgwt.client.util.Pattern;

/**
 * Checks that the entry is coherent with a money notation.
 * 
 * @author Elian ORIOU
 * 
 */

public class MoneyChecker implements IChecker, ISingleton {

	private static final MoneyChecker INSTANCE = new MoneyChecker();

	private static final Pattern MONEY_PATTERN = Pattern
			.compile("^([+]|[-])?\\d+$");

	protected MoneyChecker() {

	}

	@Override
	public boolean check(IClientValidatorContext context, String s) {

		if (MONEY_PATTERN.matches(s)) {
			return true;
		}
		context.addGlobalMessage(context.getMessagesProvider()
				.GenericErrorShort(), context.getMessagesProvider()
				.GenericError(), Severity.ERROR);
		return false;
	}

	public static IValidatorElement getInstance() {
		return INSTANCE;
	}

}
